//
//  RCUserPhoneRegVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-25.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCUserPhoneRegVC.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "RCUserPhoneRegVerifyCodeVC.h"
#import "IManager+RegisterRequest.h"
#import "RCPhoneVerifyDataModel.h"
#import "AppDelegate.h"


#define MAXLENGTH 11

@interface RCUserPhoneRegVC ()<IManagerDelegate,UITextFieldDelegate>{
    UITextField *_userNameLabel;
    UIButton    *_regBtn;
    
    int64_t     _reqID;
}

@end

@implementation RCUserPhoneRegVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    [super dealloc];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;

    UIImageView *bg1 = [[[UIImageView alloc] initWithFrame:CGRectMake(14, 17, 320-14*2, 47)] autorelease];
    UIImage *img1 = [UIImage imageNamed:@"form_bg1"];
    bg1.image = img1;
    [self.view addSubview:bg1];

    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 17, 70, 16)];
    [bg1 addSubview:phoneLabel];
    [phoneLabel setFont:[UIFont fontWithName:kFontNameLightHeiti size:16]];
    phoneLabel.textColor = [UIColor colorWithHex:0xff979797];
    phoneLabel.text = @"手机号";
    [phoneLabel release];
    
    
    _userNameLabel = [[UITextField alloc] initWithFrame:CGRectMake(100, 30, 200, 20)];
    [self.view addSubview:_userNameLabel];
    [_userNameLabel setBackgroundColor:[UIColor clearColor]];
    _userNameLabel.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userNameLabel.font = [UIFont systemFontOfSize:16];
    _userNameLabel.placeholder = @"请输入11位手机号";
//    _userNameLabel.tag = kTagUserName;
    _userNameLabel.returnKeyType = UIReturnKeyDone;
    _userNameLabel.delegate = self;
    [_userNameLabel release];
    
    
    _regBtn = [[UIButton alloc] initWithFrame:CGRectMake(14, 90, 320-28, 40)];
    [_regBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.view addSubview:_regBtn];
    UIImage *loginIMG = [UIImage imageNamed:@"login_btn"];
    [_regBtn setBackgroundImage:loginIMG forState:UIControlStateNormal];
    //        [loginBtn setBackgroundColor:[UIColor blackColor]];
    [_regBtn addTarget:self action:@selector(regPress:) forControlEvents:UIControlEventTouchUpInside];
    [_regBtn release];
    
}



- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) regPress:(id)sender
{
    NSString *phoneNumber = _userNameLabel.text;
    
    if (phoneNumber == nil) {
        return;
    }
    NSError * error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[1][3-8]+\\d{9}" options:0 error:&error];
    NSTextCheckingResult * result = [regex firstMatchInString:phoneNumber options:0 range:NSMakeRange(0, [phoneNumber length])];
    if (!result || (regex.numberOfCaptureGroups + 1) != result.numberOfRanges) {
        [RCAppDelegate alertWithCustomizeString:@"手机号码格式错误"];
        return;
    }
    

    
    if (phoneNumber.length == MAXLENGTH) {
       _reqID = [[IManager sharedInstance] requestRegVerifyCodePhoneNumber:phoneNumber withDelegate:self];
    }
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [RCAppDelegate alertWithCustomizeString:@"提交手机号码失败"];

}


- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    RCPhoneVerifyDataModel *model = (RCPhoneVerifyDataModel *)dataModel;
    if (model.errorNO == RCErrorTypeNoError) {
        RCUserPhoneRegVerifyCodeVC *verify = [[RCUserPhoneRegVerifyCodeVC alloc] initWithRegPhone:_userNameLabel.text];
        [self.navigationController pushViewController:verify animated:YES];
        [verify release];
    }else{
        [RCAppDelegate alertWithCustomizeString:model.errorMsg];
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.location >= MAXLENGTH) {
//        textField.text = [textField.text substringToIndex:MAXLENGTH-1];
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self regPress:nil];
    return YES;
}

@end
